package authorizationAdmin

import (
	"context"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/module/authorizationAdmin/logic"
	"grpc.getaway.adminspro.padmins.com/pkg/config/businessError"
	pbAuthorizationAdmin "grpc.getaway.adminspro.padmins.com/pkg/proto/authorizationAdmin"
	"strings"
)

func (rpc *auth) GetResource(ctx context.Context, pb *pbAuthorizationAdmin.GetResourceReq) (*pbAuthorizationAdmin.GetResourceResp, error) {
	rows, e := logic.GetResourceByAdminId(ctx, pb.ResourceIds)

	if e != nil {
		return nil, businessError.ErrorInfoToRpcError(e)
	}

	Resources := make([]*pbAuthorizationAdmin.Resource, len(rows))

	for i, row := range rows {
		if row["resource"] == "" {
			row["resource"] = strings.ReplaceAll(row["path"].(string), "/", ".")
		}
		Resources[i] = &pbAuthorizationAdmin.Resource{
			Id:         row["id"].(int64),
			Pid:        row["pid"].(int64),
			Type:       int32(row["type"].(int64)),
			Path:       "/" + strings.Trim(row["path"].(string), "/"),
			Resource:   row["resource"].(string),
			Title:      row["title"].(string),
			Icon:       row["icon"].(string),
			Remark:     row["remark"].(string),
			Badge:      row["badge"].(string),
			Weigh:      int32(row["weigh"].(int64)),
			Status:     int32(row["status"].(int64)),
			CreateTime: row["create_time"].(string),
			UpdateTime: row["update_time"].(string),
		}
	}

	return &pbAuthorizationAdmin.GetResourceResp{
		Resources: Resources,
	}, nil
}
